package com.lyf.viewapp.util;

import java.io.File;
import java.io.FileOutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 *  字符串相关判断
 */
public class StringUtils {

	public static boolean isEmpty(String str) {

		return "".equals(str) || null == str;
	}

	public static boolean isBlank(Object obj) {
		boolean back = obj == null ? true : false;
		if (!back)
			back = obj.toString().trim().equals("") ? true : false;
		if (!back)
			back = obj.toString().toLowerCase().equals("null") ? true : false;
		return back;
	}

	public static boolean isNotBlank(Object obj) {
		boolean back = obj == null ? false : true;
		if (back)
			back = obj.toString().trim().equals("") ? false : true;
		if (back)
			back = obj.toString().toLowerCase().equals("null") ? false : true;
		return back;
	}

	public static String formatDate(String str) {
		SimpleDateFormat sf1 = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat sf2 = new SimpleDateFormat("yyyy年MM月dd日");
		String sfstr = "";
		try {
			sfstr = sf1.format(sf2.parse(str));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return sfstr;
	}

	public static String listToString(List<String> lists) {
		StringBuilder sb = new StringBuilder();
		for (int i=0;i < lists.size();i ++) {
			sb.append(lists.get(i));
			if (i != lists.size()-1) {
				sb.append(",");
			}
		}
		return sb.toString();
	}

	public static String DateformatString(String str) {
		SimpleDateFormat sf1 = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat sf2 = new SimpleDateFormat("yyyy年MM月dd日");
		String sfstr = "";
		try {
			sfstr = sf2.format(sf1.parse(str));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return sfstr;
	}
	
	public static String DateformatNewDate(String str) {
		SimpleDateFormat sf1 = new SimpleDateFormat("yyyyMMddHHmmss");
		SimpleDateFormat sf2 = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
		String sfstr = "";
		try {
			sfstr = sf2.format(sf1.parse(str));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return sfstr;
	}
	
	public static String DateformatStandardDate(String str) {  //转换为标准时间
		SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sf2 = new SimpleDateFormat("yyyyMMddHHmmss");
		String sfstr = "";
		try {
			sfstr = sf2.format(sf1.parse(str));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return sfstr;
	}
	public static String DateformatStandardDateto(String str) { // 转换为标准时间
		SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sf2 = new SimpleDateFormat("yyyyMMddHHmmss");
		String sfstr = "";
		try {
			sfstr = sf1.format(sf2.parse(str));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sfstr;
	}
	/*
	Copy 数据库
	 */
	public static void copyfile(File fromFile, File toFile, Boolean rewrite) {
		if (!fromFile.exists()) {
			return;
		}
		if (!fromFile.isFile()) {
			return;
		}

		if (!fromFile.canRead()) {
			return;
		}
		if (!toFile.getParentFile().exists()) {
			toFile.getParentFile().mkdirs();
		}
		if (toFile.exists() && rewrite) {
			toFile.delete();
		}

		// 　　当文件不存时，canWrite一直返回的都是false
		// if (!toFile.canWrite()) {
		// MessageDialog.openError(new Shell(),"错误信息","不能够写将要复制的目标文件" +
		// toFile.getPath());
		// Toast.makeText(this,"不能够写将要复制的目标文件", Toast.LENGTH_SHORT);
		// return ;
		// }
		try {
			java.io.FileInputStream fosfrom = new java.io.FileInputStream(
					fromFile);
			FileOutputStream fosto = new FileOutputStream(toFile);
			byte bt[] = new byte[1024];
			int c;
			while ((c = fosfrom.read(bt)) > 0) {
				fosto.write(bt, 0, c); // 将内容写到新文件当中
			}
			fosfrom.close();
			fosto.close();
		} catch (Exception ex) {

		}
	}
}
